<template>
  <div>
    <!-- 月度销量查询-->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <FilterTables
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :isSelectColumn="false"
      :datakey="'records'"
      columnsRef="zjMonthlySales"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(1)">导 出</multiuse-button>
      </template>
    </FilterTables>

    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher @close="dealercloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="isProject" :title="'选择项目'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AccountProject :rowData="formData" @close="ProjectClose" />
    </BaseDialog>
    <Drawer :visible.sync="detailVisible" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="formData" @close="detailcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import {
  getList,
  postExcel,
  getNotPage,
  subOrderById,
  exporturl,
  getDeptList,
  vehicleConfiguration,
  gettmFiAccountType,
  getttFiAccount,
  getOfficeList,
  gettmVsWarehouse
} from '@/api/omSale/OrderManagement/MonthlySales/index.js';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils';
import ownerSeacher from './ownerSeacher';
import AccountProject from './AccountProject';
import detailModel from './detailModel';
import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';
import moment from 'moment';
export default {
  name: 'acknowledgementOrder',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        orderStatusList: [60021003, 60021004, 60021005, 60021006, 60021008, 60021010],
        xhrAbort: true
      },
      formData: {},
      issearch: false,
      detailVisible: false,
      isAccountProject: false,
      isProject: false,
      ProjectId: ''
    };
  },
  mixins: [dictionary, dealerMixin],
  components: {
    ownerSeacher,
    AccountProject,
    detailModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    // this.gettmVsWarehouse();
    // this.getvehicleList();
    // this.gettmFiAccountType();
    this.getDeptList();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCodes',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },

        {
          type: 'SELECT',
          label: '事业部',
          fieldName: 'deptId',
          placeholder: '请选择',
          itemList: [],
          change: p => {
            if (p) {
              this.topFilterList.map(x => {
                if (x.fieldName == 'officeId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
              this.getOfficeList(p);
            } else {
              this.topFilterList.map(x => {
                if (x.fieldName == 'officeId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
            }
          }
        },
        {
          type: 'SELECT',
          label: '办事处',
          fieldName: 'officeId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vins',
          placeholder: '请选择',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'RANGE_DATE',
          label: '统计日期',
          fieldName: 'statisticsDateBegin|statisticsDateEnd',
          placeholder: '请选择',
          initialValue: [
            moment(new Date())
              .subtract(24, 'month').add(1, 'day')
              .format('YYYY-MM-DD'),
            moment(new Date()).format('YYYY-MM-DD')
          ],
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '所属公司',
          fieldName: 'powerCompany',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  详情
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '办事处',
          dataIndex: 'officeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '所属公司',
          dataIndex: 'powerCompany',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: props => {
            return <span>{this.createDictText(props.row.powerCompany, '6008')}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订货单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '销售订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '采购退货单号',
          dataIndex: 'returnNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: p => {
            return <span>{this.createDictText(p.row.orderType, '6006')}</span>;
          }
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '订单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '待财务审核', value: 60021003 },
            { text: '财务审核通过', value: 60021004 },
            { text: '已出库', value: 60021005 },
            { text: '已发运', value: 60021006 },
            { text: '已收车入库', value: 60021008 },
            { text: '已采购退货', value: 60021010 }
          ],
          render: p => {
            return <span>{this.createDictText(p.row.orderStatus, '6002')}</span>;
          }
        },
        {
          title: '资金类型',
          dataIndex: 'accountTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户项目编号',
          dataIndex: 'projectCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户项目名称',
          dataIndex: 'majorProjectName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '收车地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系电话',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '要求交付日期',
          dataIndex: 'requiredDeliveryDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '分配数量',
          dataIndex: 'matchCount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '公告号',
          dataIndex: 'noticeNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '工厂仓库',
          dataIndex: 'parentWarehouse',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库名称',
          dataIndex: 'childWarehouse',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '市场指导价',
          dataIndex: 'singleAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '国补金额',
          dataIndex: 'subsidyAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣金额',
          dataIndex: 'discountAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣率(%)',
          dataIndex: 'discount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否使用返利',
          dataIndex: 'isRebate',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isRebate, '1004')}</span>;
          }
        },
        {
          title: '返利使用金额',
          dataIndex: 'rebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单车返利金额',
          dataIndex: 'singleRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '建店返利金额',
          dataIndex: 'buildRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折后单价',
          dataIndex: 'afterDiscountAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '账户可用余额',
          dataIndex: 'availableAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6004'),
          render: p => {
            return <span>{this.createDictText(p.row.transportType, '6004')}</span>;
          }
        },
        {
          title: '入库日期',
          dataIndex: 'receiveDates',
          sorter: true,
          filter: true,
          filterType: 'date-range',

          render: p => {
            return <span>{p.row.receiveDate ? moment(p.row.receiveDate).format('YYYY-MM-DD') : ''}</span>;
          }
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '审核人',
          dataIndex: 'checkPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '审核时间',
          dataIndex: 'checkDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '分配时间',
          dataIndex: 'matchDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '订单确认时间',
          dataIndex: 'confirmDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '财务审核时间',
          dataIndex: 'financeCheckDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: 'sap出库时间',
          dataIndex: 'sapOutDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发运下达时间',
          dataIndex: 'commandDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '采购退货日期',
          dataIndex: 'returnDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '启运时间',
          dataIndex: 'startDeliveryDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '在途位置信息',
          dataIndex: 'transPosition',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '到店交付时间',
          dataIndex: 'arriveDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '经销商收车时间',
          dataIndex: 'receiveDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-DD'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    selectChangeHandle(row) { },
    seacherFun(type) {
      if (type == 1) {
        this.issearch = true;
      } else {
        this.isProject = true;
      }
    },
    editHandle(data) {
      this.formData = data;
      this.detailVisible = true;
    },
    ProjectClose(val) {
      if (val) {
        this.topFilterList.map(x => {
          if (x.fieldName == 'majorProjectName') {
            x.initialValue = val.projectName;
            this.ProjectId = val.projectCode;
          }
        });
      }
      this.isProject = false;
    },
    dealercloseHandler(val, data) {
      if (data) {
        let dealerCodes = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCodes').initialValue = dealerCodes.toString();
      }
      this.issearch = false;
    },
    // 获取车型车系
    async getvehicleList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'brandProductList').itemList = res.data;
    },
    async getDeptList() {
      let res = await getDeptList({ orgType: 15061005 });
      let arr = res.data.map(x => ({ text: x.orgName, value: x.orgId }));
      this.topFilterList.find(x => x.fieldName == 'deptId').itemList = arr;
    },
    async getOfficeList(orgid) {
      let res = await getOfficeList({ orgType: 15061007, parentOrgId: orgid });
      let arr = res.data.map(x => ({ text: x.orgName, value: x.orgId }));
      this.topFilterList.find(x => x.fieldName == 'officeId').itemList = arr;
      this.topFilterList.find(x => x.fieldName == 'officeId').initialValue = '';
    },
    async getNotPage(orderType) {
      let list = await getNotPage({ orderType: orderType });
      this.topFilterList.map(x => {
        if (x.fieldName == 'orderDetailType') {
          x.itemList = list.data.map(x => ({ text: x.subName, value: x.subId }));
        }
      });
    },
    async gettmFiAccountType() {
      let res = (await gettmFiAccountType({ fundsType: 0 })).data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.topFilterList.find(x => x.fieldName == 'accountType').itemList = res;
    },
    async getttFiAccount(dealerCode) {
      let res = (await getttFiAccount({ ownerCode: dealerCode })).data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.topFilterList.find(x => x.fieldName == 'accountType').itemList = res;
    },
    async addHandle(type) {
      let json = Object.assign({}, { CODE: 'CODE_0000006' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.fetchParams.projectCode = this.ProjectId;
      //     let json = this.fetchParams;
      //     postExcel(json)
      //       .then(response => {
      //         exportExcelFile(response.data, '月度销量');
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });   
      //   })
      //   .catch(() => {
      //     console.log('456');
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    changeHandle(params) {
      if (this.checkTime(params.statisticsDateBegin, params.statisticsDateEnd, 24)) {
        return this.$notify.warning({ title: '提示信息', message: '起止日期跨度不能超过两年' });
      }
      this.fetchParams.projectCode = this.ProjectId;
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async gettmVsWarehouse() {
      let res = (await gettmVsWarehouse({})).data.map(x => ({ text: x.warehouseName, value: x.id }));
      this.topFilterList.find(x => x.fieldName == 'parentWarehouseId').itemList = res;
    },
    detailcloseHandler(val) {
      this.detailVisible = false;
    },
    resetChange(val) {
      this.fetchParams.projectCode = '';
      this.ProjectId = '';
      this.topFilterList.map(x => {
        if (x.fieldName == 'officeId') {
          x.itemList = [];
        }
        if (x.fieldName == 'orderDetailType') {
          x.itemList = [];
        }
      });
      this.gettmFiAccountType();
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
